/*
 * Golazo MA4
 * Copyright (C) 2008 Golazo MASHUP AWARD 4th team.
 * http://code.google.com/p/golazo/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * checkEmail
 */
function checkEmail() {

  var email = $('#form_email');
  var message = $('#form_message')
    .hide();

  var val = email.val();
  if (val != '' && !Golazo.User.checkEmail(val)) {
    
	message
	  .text('メールアドレスに誤りがあります。メールアドレスを確認してください。')
	  .show();
	
	return false;
  }

  return true;
};
checkEmail.runat = 'both';

/**
 * checkNickname
 */
function checkNickname() {

  var nickname = $('#form_nickname');
  var message = $('#form_message')
    .hide();
	
  var val = nickname.val();
  if (val != '' && !Golazo.User.checkNickname(val)) {
    
	message
	  .text('ニックネームに誤りがあります。ニックネームを確認力してください。')
	  .show();
	
	return false;
  }

  return true;
};
checkNickname.runat = 'both';

/**
 * checkNote
 */
function checkNote() {

  var note = $('#form_note');
  var message = $('#form_message')
    .hide();
	
  var val = note.val();
  if (val != '' && !Golazo.User.checkNote(val)) {
    
	message
	  .text('自己紹介に誤りがあります。自己紹介を確認してください。')
	  .show();
	
	return false;
  }

  return true;
};
checkNote.runat = 'both';

/**
 * serverload
 */
function serverload() {

  // dispatch 
  Golazo.dispatch(function(request, response, session) {

    // 私は誰か
    var me = Golazo.User.find(session.get('me'));
	if (!me)
	  return $('#signin')
	    .layout(/*me*/)
	    .metaRefresh('/signin.html?' + $.param({ url: request.uri }), 3);

    // GET
    if (request.method != 'POST') {
      
      var signature = $('#form_signature')
        .val(me.sign());

      var email = $('#form_email')
        .val(me.email);

      var nickname = $('#form_nickname')
        .val(me.nickname);

	  var note = $('#form_note')
        .val(me.note);

	  return $('#form')
	    .layout(me);
	
	// POST
	} else {

	  // 署名は期待どおりか
      var signature = $('#form_signature')
        .val(request.data.signature || '');

	  if (signature.val() !== me.sign())
	    throw new Error('(signature)');

      var email = $('#form_email')
        .val(request.data.email || '');

      var nickname = $('#form_nickname')
        .val(request.data.nickname || '');

      var note = $('#form_note')
        .val(request.data.note || '');

      // メールアドレスは期待どおりか
	  if (!checkEmail())
        return $('#form')
		  .layout(me);

      // ニックネームは期待どおりか
      if (!checkNickname())
        return $('#form')
		  .layout(me);

      // 自己紹介は期待どおりか
      if (!checkNote())
        return $('#form')
		  .layout(me);

      // 会員情報を保存する
	  try {
		me.email = email.val();
	    me.nickname = nickname.val();
	    me.note = note.val();
	    me.save();
	  } catch (e) {
	    throw new Error(''+e);
	  }

      return $('#succeeded')
	    .layout(me)
        .metaRefresh('/account.html', 3);
	}
  });
};

/**
 * load
 */
function load() {
  $(document.form).submit(function() {
    return checkEmail() && checkNickname() && checkNote();
  });
};
load.runat = 'client';